package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.Designer;
import com.ysd.util.DBManager;
public class DesignerDaolmpl {
	public List<Designer> getDesigner(){// ʵ����,����: ʵ������DaoImpl
		List<Designer> list = new ArrayList<Designer>();
		// 1.��������
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {// ����
			// 2.����Statement����
			st = conn.createStatement();
			// 3.sql���
			String sql = "select * from designer";
			// 4.ִ��sql���S
			rs = st.executeQuery(sql);
			// 5.���������S
			while(rs.next()) {
				int designerid = rs.getInt("designerid");
				String designername = rs.getString("designername");
				String designerposition = rs.getString("designerposition");
				String designerhead =rs.getString("designerhead");
				Designer desiger = new Designer(designerid, designername, designerposition,designerhead);
				list.add(desiger);
				
					
			}
			return list;// ����ִ�з����û�����
		} catch (SQLException e) {// �쳣
			e.printStackTrace();
			return null;// �쳣ִ�з��ؿ�
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	public int delDesignerId(int dgId) {
		return DBManager.executeUpdate("DELETE FROM designer where designerid="+dgId);
		
	}
	public int addDesignerId(Designer de) {
		return DBManager.executeUpdate("insert into designer values(null,'"+de.getDesignername()+"','"+de.getDesignerposition()+"','"+de.getDesignerhead()+"')");
	}
	public List<Designer> getdesignername(String name){
		List<Designer> de= new ArrayList<Designer>();
		Connection conn =DBManager.getConnection();
		Statement st =null;
		ResultSet rs =null;
		try {
			st = conn.createStatement();
			rs =st.executeQuery("select * from designer where designername='"+name+"'");
			while (rs.next()) {
				int designerid = rs.getInt("designerid");
				String designername = rs.getString("designername");
				String designerposition = rs.getString("designerposition");
				String designerhead =rs.getString("designerhead");
				Designer desiger = new Designer(designerid, designername, designerposition,designerhead);
				de.add(desiger);
			}
			return de;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
			
		}finally {
			DBManager.closeAll(conn, st, rs);
			
		}
	}
	public Designer  getdesignerid(int cateId) {
		Designer dc = null;
		Connection conn = DBManager.getConnection();
		Statement st =null;
		ResultSet rs =null;
		try {
			st = conn.createStatement();
			rs =st.executeQuery("select * from designer where designerid="+cateId);
			while (rs.next()) {
				int dId = rs.getInt(1);
				String dname =rs.getString(2);
				String dpo =rs.getString(3);
				String dimg =rs.getString(4);
				 dc = new Designer(dId, dname, dpo, dimg);
			}
			return dc;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
			
		}
	}
	
	
	public int updateDesigner(Designer dc) {
		String sql ="update designer set designername='"+dc.getDesignername()+"', designerposition='"+dc.getDesignerposition()+"', designerhead='"+dc.getDesignerhead()+"' where designerid="+dc.getDesignerid();
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
}
